package persistencia;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import modelo.Cliente;
import modelo.Funcionario;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebS {

	public List<Cliente> _teste() throws IOException, XmlPullParserException {

		String NAMESPACE = "http://tempuri.org/";
		String METHOD_NAME = "RetornoItens";
		String SOAP_ACTION = "http://tempuri.org/intrack";
		//String URL = "http://192.168.0.15/intrack/wsteste.asmx";
		String URL = "http://www.natalbot.in.cyan.mysitehosted.com/wsMobbli.asmx";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapObject result = (SoapObject) envelope.getResponse();

		List<Cliente> nomes = new ArrayList<Cliente>();
		for (int i = 0; i < result.getPropertyCount(); i++) {

			SoapObject parqueSoap = (SoapObject) result.getProperty(i);
			String idCliente = parqueSoap.getProperty("idCliente").toString();
			String nome = parqueSoap.getProperty("Nome").toString();
			String endereco = parqueSoap.getProperty("Endereco").toString();

			Cliente c = new Cliente(idCliente, nome, endereco);
			nomes.add(c);
		}
		return nomes;
	}

	public Funcionario logar(String strLogin, String strSenha) throws Exception, XmlPullParserException {

		String NAMESPACE = "http://tempuri.org/";
		String METHOD_NAME = "Autenticacao";
		String SOAP_ACTION = "http://tempuri.org/intrack";
		String URL = "http://www.natalbot.in.cyan.mysitehosted.com/wsMobbli.asmx";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("strLogin", strLogin);
		request.addProperty("strSenha", strSenha);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapObject result = (SoapObject) envelope.getResponse();

		Funcionario oFuncionario = new Funcionario();
  		oFuncionario.setIdFuncionario(result.getProperty("idFuncionario").toString());
		oFuncionario.setIdEquipe(result.getProperty("idEquipe").toString());
		oFuncionario.setNome(result.getProperty("Nome").toString());
		oFuncionario.setLogin(result.getProperty("Login").toString());
		oFuncionario.setSenha(result.getProperty("Senha").toString());
		oFuncionario.setResponsavel(result.getProperty("Responsavel").toString());

		return oFuncionario;
	}
	
	public String atualizarCoordenadas(String strChave, int idCliente, String lat, String lon) throws Exception, XmlPullParserException {

		String NAMESPACE = "http://tempuri.org/";
		String METHOD_NAME = "InserirCoordenadas";
		String SOAP_ACTION = "http://tempuri.org/intrack";
		//String URL = "http://192.168.0.15/intrack/wsMobbli.asmx";
		String URL = "http://www.natalbot.in.cyan.mysitehosted.com/wsMobbli.asmx";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("strChave", strChave);
		request.addProperty("idCliente", idCliente);
		request.addProperty("lat", lat);
		request.addProperty("lon", lon);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		
		String resultado = result.getAttributeAsString(0);
		
		return resultado;
	}
}